//
//  LabelCollectionViewLayout.m
//  15_Work
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "LabelCollectionViewLayout.h"

@interface LabelCollectionViewLayout ()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributesArray;
@property (nonatomic, assign) CGFloat itemHeight;
@property (nonatomic, assign) CGFloat maxWidth;

@end

@implementation LabelCollectionViewLayout

- (instancetype)init {
    self = [super init];
    if (self) {
        _attributesArray = [NSMutableArray array];
        _numOfColumn = 2;
        _itemHeight = 40;
        _maxWidth = 20;
    }
    return self;
}


- (void)prepareLayout {
    for (NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        CGFloat width = [self.delegate collectionView:self.collectionView layout:self height:_itemHeight widthForItemAtIndexPath:index];
        CGFloat x = _maxWidth;
        CGFloat y = 0;
        CGFloat height = _itemHeight;
        attributes.frame = CGRectMake(x, y, width, height);
        [self.attributesArray addObject:attributes];
        _maxWidth = _maxWidth + width + 20;
    }
}

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.attributesArray;
}


- (CGSize)collectionViewContentSize {
    return CGSizeMake(_maxWidth, 0);
}












@end
